package com.project.capture5.app;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * @author Shelly An
 * @create 2020/9/18 15:19
 */
public class PageViewByFlatmap {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        //1. 从文件读取数据，转换成bean对象
        DataStreamSource<String> fileDS = env.readTextFile("Data/UserBehavior.csv");


        SingleOutputStreamOperator<Tuple2<String, Integer>> pvFilterDS = fileDS.flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public void flatMap(String value, Collector<Tuple2<String, Integer>> out) throws Exception {
                String[] datas = value.split(",");
                if ("pv".equals(datas[3])) {
                    out.collect(Tuple2.of("pv", 1));
                }
            }
        });



        //2.2 按照统计的维度分组 ： pv行为
        KeyedStream<Tuple2<String, Integer>, Tuple> pvKS = pvFilterDS.keyBy(0);

        //2.3 求和
        SingleOutputStreamOperator<Tuple2<String, Integer>> resultDS = pvKS.sum(1);

        resultDS.print("pv by flatmap");

        env.execute();
    }
}
